<template>
  <div class="relative">
    <page-loading></page-loading>
    <template v-for="(item, idx) in pages[0].fields">
      <os-switch v-if="idx==1" :key='idx' :item='item'
      v-model="firewall_state[idx]" class="specify-switch"></os-switch>
      <os-switch  v-if="idx!=1" :key='idx' :item='item'
      v-model="firewall_state[idx]"></os-switch>
    </template>

    <!-- <os-switch v-for="(item, idx) in pages[0].fields"
    :key='idx' :item='item' :value="firewall_state[5]=='REJECT'"
    v-if='idx==5' @handleSwitch="toggle"></os-switch> -->
    <div class="value alone-right">
      <div class="btn btn-blue" @click="set_firewall">{{$t('btn.save')}}</div>
    </div>
  </div>
</template>

<script>
  import tab from '@/components/tab'
  // import osTextarea from '@/components/form/os-textarea'
  export default {
    // components: {
    //   tab,
    // },
    data(){
      return {
        pages: this.$t('advanced.pages'),
        firewall_state: '',
      }
    },
    computed:{
      perm: {
        get(){
          return this.$store.state.info.perm.adv
        },
        set(){

        },
      },
    },
    methods: {
      get_firewall(){
        this.$req.get('/webapi?op=get_fw_setting').then(res => {
          console.log(res)
          // res.data.Prohibit_ping = res.data.Prohibit_ping == '' ? 'REJECT' : res.data.Prohibit_ping
          this.firewall_state = this.obj_to_arr(res.data, ['drop_invalid',
          'syn_flood', 'disable_ipv6',
          'accept_redirects', 'prohibit_wan_ping'])
          console.log(this.firewall_state)
        })
      },
      set_firewall(){
        let params = this.arr_to_obj(this.firewall_state, ['drop_invalid',
        'syn_flood', 'disable_ipv6', 
        'accept_redirects', 'prohibit_wan_ping'])
        // let protect = params.synflood_protect
        // params.synflood_protect = protect == '0' || protect == '1' ? protect : '0'
        Object.assign(params, {
          // rulename: 'Prohibit',
          src: 'wan',
          dest: 'all',
          target: params.prohibit_wan_ping == '1' ? 'ACCEPT' : 'REJECT',
        })
        console.log(params)
        this.proxy_req('post','/webapi?op=set_fw_setting', params, this.get_firewall, true)
      },
      toggle(){
        let value = this.firewall_state[5] == 'ACCEPT' ? 'REJECT' : 'ACCEPT'
        this.firewall_state.splice(5, 1, value)
      }
    },
    created(){
      this.get_firewall()
      this.$bus.emit('changeTab', 0)
    },
    deactivated() {

    },
    activated() {
      // 同步二级路由
      this.$bus.emit('changeTab', 0)
    },
  }
</script>

<style lang="scss" scoped>
  .specify-switch ::v-deep .field{
    padding-left: 0;
  }
</style>
